package encrypt

import (
	"crypto/aes"
	"crypto/cipher"
)

var (
	//秘钥长度需要时AES-128(16bytes)或者AES-256(32bytes)
	secretKey = "jfsfkshlhglmskllgsfjlafd;e4*23)4"

	//填充：原文必须填充至blocksize的整数倍，填充方法可以参见https://tools.ietf.org/html/rfc5246#section-6.2.3.2
	commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
)

//AES加密字符串
func AesEncrypt(plaintext []byte) ([]byte, error)  {
	// 创建加密算法aes
	c, err := aes.NewCipher([]byte(secretKey))
	if err != nil {
		return nil, err
	}

	//加密字符串
	cfb := cipher.NewCFBEncrypter(c, commonIV)
	ciphertext := make([]byte, len(plaintext))
	cfb.XORKeyStream(ciphertext, []byte(plaintext))

	return ciphertext, nil
}


//AES解密字符串
func AesDecrypt(ciphertext []byte) ([]byte, error) {
	// 创建加密算法aes
	c, err := aes.NewCipher([]byte(secretKey))
	if err != nil {
		return nil, err
	}

	//解密字符串
	cfbdec := cipher.NewCFBDecrypter(c, commonIV)
	plaintext := make([]byte, len(ciphertext))
	cfbdec.XORKeyStream(plaintext, []byte(ciphertext))

	return plaintext, nil
}
